﻿<?php
class Log{
    private static $log_instance;
    private static $ERROR_DIR;

    private function __construct(){
        self::$ERROR_DIR = dirname(__FILE__).'/../error_log/';
        if(!file_exists(self::$ERROR_DIR))
            mkdir('error_log');
    }
    public static function getInstance(){
        if(self::$log_instance == null)
        {
            self::$log_instance = new Log();
            return self::$log_instance;
        }
        else
            return self::$log_instance;
    }	
    private function get_debug_print_backtrace(){
        $traces = debug_backtrace();
        ob_start();
        debug_print_backtrace();
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }
    public function log($message){
        date_default_timezone_set('Asia/Shanghai');
        $date = date('Y-m-d H:i:s');
        $file_name = self::$ERROR_DIR.date('Ymd').'.log';
        $content = "\n\n".$date." | ".$message."\n".$this->get_debug_print_backtrace();
        if(!is_file($file_name))
            $content = "\xEF\xBB\xBF".$content;
        error_log($content,3,$file_name);
    }
}
?>